#include <iostream>
#include "logger.hpp"
#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"

#define HOST "localhost"
#define USER "JK"
#define PASSWORD "jiangJIANG010513"
#define DB "online_gobang"
#define PORT 3306

//日志测试
void LogTest()
{
    ILOG("nihao");
    const char* str="hello world";
    ILOG("%s",str);
}

//MySQL工具类测试
void MySQLTest()
{
    MYSQL* mysql=mysql_util::mysql_create(HOST,USER,PASSWORD,DB,PORT);
    const char* str="insert into test values(1,'孙悟空');";
    mysql_util::mysql_exec(mysql,str);
    mysql_util::mysql_destroy(mysql);
}

//Json工具测试
void JsonTest()
{
    Json::Value root;
    root["姓名"]="小明";
    root["年龄"]=19;
    root["成绩"].append(77.5);
    root["成绩"].append(80.5);
    root["成绩"].append(90.5);
    std::string str;
    json_util::serialize(root,str);
    std::cout<<str<<std::endl;
    Json::Value ret;
    json_util::unserialize(str,ret);
    std::cout<<"姓名: "<<ret["姓名"].asString()<<std::endl;
    std::cout<<"年龄: "<<ret["年龄"].asInt()<<std::endl;
    for(int i=0;i<3;i++)
    {
        std::cout<<"成绩: "<<ret["成绩"][i].asFloat()<<std::endl;
    }
}

//split分割字符串函数测试
void SplitTest()
{
    const char* str="123,456,,,,,789,";
    std::vector<std::string> ret;
    string_util::split(str,",",ret);
    for(auto& s:ret)
    {
        std::cout<<s<<std::endl;
    }
}

//测试user_table类
void UserTableTest()
{
    user_table ut(HOST,USER,PASSWORD,DB,PORT);
    Json::Value root;
    root["username"]="孙悟空";
    root["password"]="12345678";
    ut.insert(root);
    ut.login(root);
    uint64_t id=root["id"].asInt64();
    std::string name=root["username"].asString();
    ut.win(id);
    ut.lose(id);
    Json::Value ur1;
    ut.select_by_id(id,ur1);
    Json::Value ur2;
    ut.select_by_name(name,ur2);

    std::cout<<"id: "<<ur1["id"].asUInt64()<<std::endl;
    std::cout<<"username: "<<ur1["username"].asString()<<std::endl;
    std::cout<<"score: "<<ur1["score"].asUInt64()<<std::endl;
    std::cout<<"total_count: "<<ur1["total_count"].asInt()<<std::endl;
    std::cout<<"win_count: "<<ur1["win_count"].asInt()<<std::endl;
    std::cout<<"-------------------"<<std::endl;
    std::cout<<"id: "<<ur2["id"].asUInt64()<<std::endl;
    std::cout<<"username: "<<ur2["username"].asString()<<std::endl;
    std::cout<<"score: "<<ur2["score"].asUInt64()<<std::endl;
    std::cout<<"total_count: "<<ur2["total_count"].asInt()<<std::endl;
    std::cout<<"win_count: "<<ur2["win_count"].asInt()<<std::endl;
}

//测试online_manager类
void OnlineTest()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid=2;
    om.enter_game_hall(uid,conn);
    if(om.is_in_game_hall(uid)) DLOG("in game hall!!!");
    else DLOG("not in game hall!!!");
    om.exit_game_hall(uid);
    if(om.is_in_game_hall(uid)) DLOG("in game hall!!!");
    else DLOG("not in game hall!!!");
    std::cout<<"------------------"<<std::endl;
    om.enter_game_room(uid,conn);
    if(om.is_in_game_room(uid)) DLOG("in game room!!!");
    else DLOG("not in room hall!!!");
    om.exit_game_room(uid);
    if(om.is_in_game_room(uid)) DLOG("in room hall!!!");
    else DLOG("not in room hall!!!");
}

//测试room_manager类
void RoomManagerTest()
{
    user_table ut(HOST,USER,PASSWORD,DB,PORT);
    online_manager om;
    room_manager rm(&ut,&om);
}

//测试session_manager类
void SessionManagerTest()
{
    wsserver_t wt;
    session_manager sm(&wt);
}

//测试matcher类
void MatcherTest()
{
    user_table ut(HOST,USER,PASSWORD,DB,PORT);
    online_manager om;
    room_manager rm(&ut,&om);
    matcher mm(&rm,&ut,&om);
}

int main()
{
    //LogTest();
    //MySQLTest();
    //JsonTest();
    //SplitTest();
    //UserTableTest();
    //OnlineTest();
    //RoomManagerTest();
    //SessionManagerTest();
    MatcherTest();
    return 0;
}